<template>
    <div class="home">

         <HomeHeader></HomeHeader>
        <Homeimg></Homeimg>
      <Homenva></Homenva>
      <Recommend></Recommend>
      <Weekend></Weekend>
    </div>
</template>

<script>
    import  HomeHeader from "./components/HomeHeader"
    import Homeimg from "./components/Homeimg";
    import Homenva from "./components/Homenva";
    import Recommend from "./components/Recommend";
    import Weekend from "./components/Weekend";
    export default {
        name:"Home",
        components:{
            HomeHeader,
          Homeimg,
          Homenva,
          Recommend,
          Weekend
        },
      data(){
          return{

          }
      },
      beforeRouteLeave(to,from,next){//当离开组件前 执行函数
        if(to.name=='detail'&&from.name=='home'){//如果他到detail并且从home里来
          from.meta.keepAlive=true;
          var scrollTop =window.pageXOffset||document.documentElement.scrollTop||document.body.scrollTop  //
          localStorage.setItem('scrollTop',scrollTop)

        }else {
          from.meta.keepAlive=false
        }
        next();
      },
      beforeRouteEnter(to,from,next){//当进入组件前 执行函数
        var position1= parseInt(localStorage.getItem("scrollTop"));
        var index =parseInt(localStorage.getItem("index"));
        next(vm=>{
          //通过'vm'访问组件实例
          if(from.name=='detail' && to.name=='home'){
            var time=setInterval(()=>{
              document.documentElement.scrollTop=position1;
              document.body.scrollTop=position1;
              vm.loading=false;
              clearInterval(time)//清除隔离
            },0)
          }
        })
      }
    }
</script>

<style lang="stylus" scoped>

</style>
